﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.Lists.BusinessLogic;

namespace GUserInterface.WinForms.Lists
{
    public partial class ColumnConfiguration : Form
    {
        private ConfiguredColumn _selectedColumn;
        private IList<Column> _availableColumns;

        public ConfiguredColumn SelectedColumn
        {
            get
            {
                if (_selectedColumn == null)
                {
                    _selectedColumn = new ConfiguredColumn();
                }
                return _selectedColumn;
            }
            set { _selectedColumn = value; }
        }

        public ColumnConfiguration(IList<Column> availableColumns)
        {
            InitializeComponent();
            _availableColumns = availableColumns;
        }

        public void InitializeColumns()
        {
            if (_availableColumns.Count == 0)
            {
                MessageBox.Show("Brak wybranych kolumn");
                DialogResult = DialogResult.Abort;
                this.Close();
            }

            cbColumn.DataSource = _availableColumns;
            cbColumn.DisplayMember = "Name";

            cbColumn.SelectedIndex = 0;

            if (SelectedColumn.Column != null)
            {
                cbColumn.SelectedItem = _selectedColumn.Column;
                tbDisplayedName.Text = _selectedColumn.DisplayName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbDisplayedName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Należy podać wyświetlaną nazwę");
                return;
            }

            _selectedColumn.Column = cbColumn.SelectedItem as Column;
            _selectedColumn.DisplayName = tbDisplayedName.Text;

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
